package work;

import java.util.Random;

public class Count_Time {
    public static void main(String[] args) throws InterruptedException {
        int count=1_0000_0000;
        int[] arr=new int[count];
        Random random=new Random();
        for (int i = 0; i <arr.length ; i++) {
            arr[i]=random.nextInt(100)+1;
        }
        Count count1=new Count(0,0);
        Thread t1 =new Thread(()->{
            for (int i = 0; i <count ; i+=2) {
                count1.add1(arr[i]);
            }
        });
        Thread t2 =new Thread(()->{
            for (int i = 1; i <count ; i+=2) {
                count1.add2(arr[i]);
            }
        });
        //
        long start=System.currentTimeMillis();
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        long end=System.currentTimeMillis();
        System.out.println("总和："+(count1.sum1+count1.sum2)+"  sum1:"+count1.sum1+"  sum2:"+count1.sum2);
        System.out.println("耗时间："+(end-start));
        long sum=0;
        long start1=System.currentTimeMillis();
        for (int i = 0; i <count ; i++) {
            sum+=arr[i];
        }
        long end1=System.currentTimeMillis();
        System.out.println("总和："+sum);
        System.out.println("耗时间："+(end1-start1));
    }
}
class Count{
    public long sum1;
    public long sum2;
    public Count(long sum1, long sum2) {
        this.sum1 = sum1;
        this.sum2 = sum2;
    }
    public void add1(int n){
        sum1+=n;
    }
    public void add2(int n){
        sum2+=n;
    }

}
